package com.zhuanzhuan.dao.impl;
import com.zhuanzhuan.dao.BaseDao;
import com.zhuanzhuan.dao.TradelogDao;
import com.zhuanzhuan.entity.Tradelog;
import com.zhuanzhuan.vo.GoodsDetail;
import com.zhuanzhuan.vo.TradelogDetail;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TradelogDaoMySQLImpl extends BaseDao implements TradelogDao {

    //添加交易记录
    @Override
    public int addTradelog(Tradelog tradelog){
        //构造SQL语句
        String preparedSQL = "INSERT INTO tradelog (buyerID,sellerID,goodsID,tradeTime,receiveInfo) VALUES (?,?,?,?,?)";
        //构造SQL执行参数数组
        List<Object> params = new ArrayList<Object>();
        params.add(tradelog.getBuyerID());
        params.add(tradelog.getSellerID());
        params.add(tradelog.getGoodsID());
        params.add(tradelog.getTradeTime());
        params.add(tradelog.getReceiveInfo());

        //调用BaseDao中的更新
        return exceuteUpdate(preparedSQL, params.toArray());
    }

    //获取交易记录
    @Override
    public List<TradelogDetail> getTradelogs(){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<TradelogDetail> tradelogs = new ArrayList<TradelogDetail>();

        conn = getConnection();
        try {
            //构造PreparedStatement对象
            String sql = "SELECT tradelog.id,buyerID,goodsID,sellerID,tradeTime,receiveInfo," +
                    "us1.name AS sellerName, us2.name AS buyerName, goods.name, goods.price FROM tradelog " +
                    "JOIN user AS us1 ON tradelog.sellerID = us1.id " +
                    "JOIN user AS us2 ON tradelog.buyerID =us2.id " +
                    "JOIN goods ON tradelog.goodsID = goods.id;";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            //验证用户名和密码
            while(rs.next()){
                //从MySQL读取用户信息，并加载到patient对象中
                TradelogDetail tradelog= new TradelogDetail();
                tradelog.setId(rs.getInt("id"));
                tradelog.setBuyerID(rs.getInt("buyerID"));
                tradelog.setGoodsID(rs.getInt("goodsID"));
                tradelog.setSellerID(rs.getInt("sellerID"));
                tradelog.setTradeTime(rs.getDate("tradeTime"));
                tradelog.setReceiveInfo(rs.getString("receiveInfo"));
                tradelog.setSellerName(rs.getString("sellerName"));
                tradelog.setBuyerName(rs.getString("buyerName"));
                tradelog.setGoodsName(rs.getString("name"));
                tradelog.setPrice(rs.getFloat("price"));
                tradelogs.add(tradelog);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭数据库连接
            closeAll(conn, pstmt, rs);
        }
        return tradelogs;
    }
}
